package studentsystem1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class AddForm extends JFrame implements ActionListener {
    JLabel labName = new JLabel("姓名：");
    JLabel labDate = new JLabel("学号：");
    JLabel labScore = new JLabel("成绩：");
    JTextField txtName = new JTextField(20);
    JTextField txtDate = new JTextField(18);
    JTextField txtScore = new JTextField(20);
    JButton btnAdd = new JButton("添加");
    JButton btnCancel = new JButton("取消");
    JPanel pan = new JPanel();
    JPanel pan1 = new JPanel();
    JPanel pan2 = new JPanel();
    JPanel pan3 = new JPanel();
    JPanel pan4 = new JPanel();
    Connection con;
    Statement sql;
    ResultSet rs;

    AddForm() {
        super("添加数据");
        setSize(400, 300);
        pan1.add(labName);
        pan1.add(txtName);
        pan2.add(labDate);
        pan2.add(txtDate);
        pan3.add(labScore);
        pan3.add(txtScore);
        pan4.add(btnAdd);
        pan4.add(btnCancel);
        pan.setLayout(new GridLayout(3, 1));
        pan.add(pan1);
        pan.add(pan2);
        pan.add(pan3);
        getContentPane().add(pan, "Center");
        getContentPane().add(pan4, "South");
        btnAdd.addActionListener(this);
        btnCancel.addActionListener(this);
        btnAdd.setEnabled(true);
        txtDate.setEditable(true);
        txtScore.setEditable(true);
        setVisible(true);
        txtName.requestFocus();
    }

    public void actionPerformed(ActionEvent ae) {
        String recode, insert1, stu_time, username;
        int stu_score;
        if (ae.getSource() == btnCancel) {
            this.dispose();
        } else if (ae.getSource() == btnAdd) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            try {

                String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
                String user = "root";
                String passwd = "123456";
                con = DriverManager.getConnection(url, user, passwd);
                sql = con.createStatement();
                username = txtName.getText();
                stu_time = txtDate.getText();
                stu_score = Integer.parseInt(txtScore.getText());
                // recode="("+"'"+username+"'"+","+"'"+stu_time+""+"',"+stu_score+")";
                //   insert1="insert into student1 values "+recode;
                insert1 = "insert into student(username,stu_time,stu_score) values('"
                        +username+"',"+stu_time+","+stu_score+")";
                sql.executeUpdate(insert1);
                System.out.println("记录添加完毕！");
                btnAdd.setEnabled(false);
                txtName.setText("");
                txtScore.setText("");
                txtDate.setText("");
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new AddForm();
    }
}